import 'package:demo/HomeWidget.dart';
import 'package:demo/ImageWidget.dart';
import 'package:demo/PictureWidget.dart';
import 'package:demo/SlidingPanelWidget.dart';
import 'package:demo/SliverWidget.dart';
import 'package:flutter/material.dart';

import 'FileWidget.dart';
import 'ListOperationWidget.dart';

// import 'package:flutter_native_splash/flutter_native_splash.dart';
void main() async {
  // WidgetsFlutterBinding.ensureInitialized();
  // await initialization(null);
  runApp(const MyApp());
}

//
// Future initialization(BuildContext? context) async {
//   await Future.delayed(const Duration(milliseconds: 500));
// }
final List<String> dataList = [
  '工会',
  '图片选择',
  '文件选择',
  'Slivers',
  '列表操作',
  '大图预览',
  '底部滑动抽屉',
];

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: const IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: null,
        ),
        title: const Text("欢迎"),
        centerTitle: true,
        toolbarHeight: 50,
        backgroundColor: Colors.white,
        titleTextStyle: const TextStyle(color: Colors.blue),
        actions: [
          GestureDetector(
            child: const Text("更多", style: TextStyle(color: Colors.blue)),
            onTap: () {
              Navigator.push(context,
                  MaterialPageRoute(builder: (context) => const HomeWidget()));
            },
          ),
          IconButton(
              onPressed: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => const ImageWidget()));
              },
              icon: const Icon(Icons.search)),
        ],
      ),
      body: Center(
        child: Column(
          // mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[getList()],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }

  Widget getList() {
    return GridView.builder(
        itemCount: dataList.length,
        shrinkWrap: true,
        padding: EdgeInsets.zero,
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3,
          mainAxisSpacing: 12,
          mainAxisExtent: 100,
          crossAxisSpacing: 20,
        ),
        itemBuilder: (context, index) {
          return GestureDetector(
            child: getItem(dataList[index]),
            onTap: () {
              switch (index) {
                case 0:
                  _jumpPage(const HomeWidget());
                  break;
                case 1:
                  _jumpPage(const ImageWidget());
                  break;
                case 2:
                  _jumpPage(const FileWidget());
                  break;
                case 3:
                  _jumpPage(const SliverWidget());
                  break;
                case 4:
                  _jumpPage(const ListOperationWidget());
                  break;
                case 5:
                  _jumpPage(const PictureWidget());
                  break;
                case 6:
                  _jumpPage(const SlidingPanelWidget());
                  break;
              }
            },
          );
        });
  }

  Widget getItem(item) {
    return Hero(
        tag: item,
        child: Container(
          alignment: Alignment.center,
          width: 100,
          height: 30,
          color: Colors.red,
          child: Text(
            item,
            style: const TextStyle(color: Colors.white, shadows: [
              Shadow(color: Colors.black, offset: Offset(.5, .5), blurRadius: 2)
            ]),
          ),
        ));
  }
  _jumpPage(Widget page) {
    Navigator.push(context, MaterialPageRoute(builder: (context) => page));
  }
}
